package cn.com.dev.temp;

import cn.com.kh.KHUtil;
import cn.com.kh.LogToDB;
import cn.com.kh.TxConfig;
import cn.com.kh.batch.FCallAPI_Utils;
import cn.com.kh.result.iAPIResult;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

/**
 * @Title: cn.com.dev-DevTao
 * @Description:ngsapitask-ph 系统API接口开发Demo，重点关注业务逻辑部分
 * @Author: lijun
 * @Date 2021-12-03 10:23
 */
public class DevDynFDDAPI {
    private static final Log log = LogFactory.getLog(DevDynFDDAPI.class);
    private static String APPID = "100002";
    private static String APPKEY = "E4uX9c65e5h7A5K5DA6rw6JL";

    public static void main(String[] args) throws Exception {
        // 获取数据库操作对象
        JdbcTemplate jt = new JdbcTemplate(TxConfig.getDruidDataSource());
        long startTime = System.currentTimeMillis();
        JSONObject res_Json = iAPIResult.ERROR("获取-个人用户信息查询 失败");
        String callPram = "{\"rec_guid\":\"" + KHUtil.getRandomString(36) + "\",\"customer_guid\":\"477b883a-1b74-4228-82ec-e81878e2d3ff\",\"customer_id\":\"00099999\",\"mobile\":\"17050083961\"}";


        int i_Num = 0;
        try {
            // 解析请求报文
            JSONObject callJson = JSONObject.fromObject(callPram);

            // 解析入参信息
            String Rec_Guid = KHUtil.GetSFJV(callJson, "rec_guid");
            String Cc_CustomerGuid = KHUtil.GetSFJV(callJson, "customer_guid");
            String Cc_CustomerId = KHUtil.GetSFJV(callJson, "customer_id");
            String Cc_Mobile = KHUtil.GetSFJV(callJson, "mobile");
            log.debug("-接收界面出来参数(注册->认证) Rec_Guid:" + Rec_Guid + " Cc_CustomerGuid:" + Cc_CustomerGuid + " Cc_CustomerId:" + Cc_CustomerId + " Cc_Mobile:" + Cc_Mobile);

            String checkSQL = "Select Customer_Guid, Customername, Fdd_Customerid, Customertype From Customer Where Customerid = ? Or Customerid2 = ? Order By Modifieddate Desc Limit 1";
            List<Map<String, Object>> checkList = jt.queryForList(checkSQL, Cc_CustomerId, Cc_CustomerId);
            log.debug("Fix-查询系统Customer表是否存在CustomerID对应客户信息:" + checkList.size());


            String Dbc_isCompany = "个人";
            String Dbc_CustomerName = "";
            if (checkList.size() > 0) {
                Dbc_isCompany = checkList.get(0).get("Customertype").toString();
                Dbc_CustomerName = checkList.get(0).get("Customername").toString();
            }
            Dbc_isCompany = KHUtil.isNull(Dbc_isCompany) ? "个人" : Dbc_isCompany;

            if (Dbc_isCompany.equals("个人")) {

                // 查询-FDD-通过手机号获取法大大用户信息
                JSONObject res_PersonInfo = FCallAPI_Utils.getFDDPersonInfo(jt, Rec_Guid, Cc_Mobile);
                log.debug("-通过手机号获取法大大当前用户信息 :" + res_PersonInfo);

                String Fdd_CustomerId = "";
                String verifiedStatus = "";
                String verifiedStatusDesc = "";

                if (KHUtil.GetSFJV(res_PersonInfo, "code").equals("1")) { // 查询到客户信息

                    // 获取法大大接口返回内容
                    JSONObject res_FddPersonInfo = res_PersonInfo.getJSONObject("data");
                    if (KHUtil.GetSFJV(res_FddPersonInfo, "code").equals("1")) {
                        if (res_FddPersonInfo.getJSONArray("data").size() > 0) {
                            Fdd_CustomerId = KHUtil.GetSFJV(res_FddPersonInfo.getJSONArray("data").getJSONObject(0), "customerId");
                            verifiedStatus = KHUtil.GetSFJV(res_FddPersonInfo.getJSONArray("data").getJSONObject(0), "verifiedStatus");
                            verifiedStatusDesc = KHUtil.GetSFJV(res_FddPersonInfo.getJSONArray("data").getJSONObject(0), "verifiedStatusDesc");

                            // 将获取到的FDD客户编号和最新认证状态更新到Customer
                            i_Num = jt.update("Update Customer Set Fdd_Customerid = ?, Fdd_Status = ?, Fdd_Reason = ?, Modifiedby = 'SELITE', Modifieddate = Now() Where Customerid = ? Or Customerid2 = ?", Fdd_CustomerId, verifiedStatus, verifiedStatusDesc, Cc_CustomerId, Cc_CustomerId);
                            log.debug("Fix-将FDD用户对应的信息更新到Customer-更新DB数据:" + i_Num);

                            // 识别FDD客户认证状态
                            if (verifiedStatus.equals("1")) { // 未认证
                                JSONObject res_PersonVerify = FCallAPI_Utils.doFDDPersonVerify(jt, Rec_Guid, Cc_CustomerId, Fdd_CustomerId, Cc_Mobile);
                                log.debug("Fix-根据用户信息 识别FDD客户认证状态-未认证,已推送认证:" + res_PersonVerify);

                                jt.update("Insert Into Sys_Oper_Log_Auth (Oper_Id, Rec_Guid, Customer_Id, Customer_Guid, Fdd_Customer_Id, Push_Type, Push_Time) Values (Uuid(), ?, ?, ?, ?, '认证', Now())", Rec_Guid, Cc_CustomerId, Cc_CustomerGuid, Fdd_CustomerId);

                                res_Json = iAPIResult.WARN("检测到客户认证状态:未认证,已推送认证,认证链接:" + KHUtil.GetSFJV(res_PersonVerify.getJSONObject("data").getJSONObject("data"), "url"), res_PersonVerify);

                            } else if (verifiedStatus.equals("2")) { // 已认证-通过
                                log.debug("Fix-根据用户信息 识别FDD客户认证状态-已认证通过");

                                JSONObject res_JsonData = new JSONObject();
                                res_JsonData.put("Fdd_CustomerId", Fdd_CustomerId);
                                res_JsonData.put("verifiedStatus", verifiedStatus);
                                res_JsonData.put("verifiedStatusDesc", verifiedStatusDesc);
                                res_Json = iAPIResult.SUCCESS("检测到客户认证状态:已认证通过,可关闭当前页面重新打开进行下一步操作", res_JsonData);

                            } else {
                                res_Json = iAPIResult.ERROR("检测到客户认证状态(未定义状态):-" + verifiedStatus + "/" + verifiedStatusDesc, res_FddPersonInfo);

                            }

                        } else { // 未查询到客户信息

                            // 未查询到客户信息 新注册客户
                            JSONObject res_PersonRegi = FCallAPI_Utils.doFDDPersonRegi(jt, Rec_Guid, Cc_Mobile);

                            if (KHUtil.GetSFJV(res_PersonRegi, "code").equals("1")) {
                                JSONObject res_FddPersonRegi = res_PersonRegi.getJSONObject("data");
                                // 如果法大大返回 1成功或1001003已存在均是成功
                                if (KHUtil.GetSFJV(res_FddPersonRegi, "code").equals("1") || KHUtil.GetSFJV(res_FddPersonRegi, "code").equals("1001003")) {
                                    Fdd_CustomerId = KHUtil.GetSFJV(res_FddPersonRegi, "data");

                                    // 将获取到的FDD客户编号更新到Customer
                                    i_Num = jt.update("Update Customer Set Fdd_Customerid = ?, Modifiedby = 'SELITE', Modifieddate = Now() Where Customer_Guid = ?", Fdd_CustomerId, Cc_CustomerGuid);
                                    log.debug("Fix-新注册客户后,FDD客户编号更新到Customer-更新DB数据:" + i_Num);

                                    // 新建客户后直接推送发起认证
                                    JSONObject res_PersonVerify = FCallAPI_Utils.doFDDPersonVerify(jt, Rec_Guid, Cc_CustomerId, Fdd_CustomerId, Cc_Mobile);
                                    log.debug("Fix-新注册客户后直接推送发起认证,推送认证:" + res_PersonVerify);

                                    jt.update("Insert Into Sys_Oper_Log_Auth (Oper_Id, Rec_Guid, Customer_Id, Customer_Guid, Fdd_Customer_Id, Push_Type, Push_Time) Values (Uuid(), ?, ?, ?, ?, '认证', Now())", Rec_Guid, Cc_CustomerId, Cc_CustomerGuid, Fdd_CustomerId);

                                    res_Json = iAPIResult.WARN("新注册客户后直接推送发起认证,已推送认证-等待认证回调,认证链接:" + KHUtil.GetSFJV(res_PersonVerify.getJSONObject("data").getJSONObject("data"), "url"), res_PersonVerify);

                                } else {
                                    res_Json = iAPIResult.ERROR("调用法大大注册客户(法大大错误)-" + KHUtil.GetSFJV(res_FddPersonRegi, "msg"), res_FddPersonRegi);

                                }

                            } else {
                                res_Json = iAPIResult.ERROR("调用法大大注册客户(内部错误)-" + KHUtil.GetSFJV(res_PersonRegi, "msg"), res_PersonRegi);
                            }

                        }

                    } else {
                        res_Json = iAPIResult.ERROR("调用法大大查询客户(法大大错误)-" + KHUtil.GetSFJV(res_FddPersonInfo, "msg"), res_FddPersonInfo);

                    }
                } else {
                    res_Json = iAPIResult.ERROR("调用法大大查询客户(内部错误)-" + KHUtil.GetSFJV(res_PersonInfo, "msg"), res_PersonInfo);

                }
            } else {

                // 查询法大大接口获取企业状态
                JSONObject res_CompanyInfo = FCallAPI_Utils.getFDDCompanyInfo(jt, Rec_Guid, Dbc_CustomerName);

                log.debug("获取[" + Dbc_CustomerName + "][" + Rec_Guid + "]法大大企业基础信息 结果：" + res_CompanyInfo);

                String Fdd_CustomerId = "";
                String Fdd_CompanyId = "";
                String verifiedStatus = "";
                String verifiedStatusDesc = "";

                if (KHUtil.GetSFJV(res_CompanyInfo, "code").equals("1")) { // 查询到企业信息

                    JSONObject res_FddCompanyInfo = res_CompanyInfo.getJSONObject("data");
                    // Code 1 法大大返回有信息 1001005其实是没有查到企业信息 这两种情况不属于错误
                    if (KHUtil.GetSFJV(res_FddCompanyInfo, "code").equals("1") || KHUtil.GetSFJV(res_FddCompanyInfo, "code").equals("1001005")) {
                        // Code 1 法大大返回有信息 解析
                        if (KHUtil.GetSFJV(res_FddCompanyInfo, "code").equals("1")) {
                            Fdd_CompanyId = KHUtil.GetSFJV(res_FddCompanyInfo.getJSONArray("data").getJSONObject(0), "customerId");
                            Fdd_CustomerId = KHUtil.GetSFJV(res_FddCompanyInfo.getJSONArray("data").getJSONObject(0).getJSONObject("companyAdminInfoVO"), "customerId");
                            verifiedStatus = KHUtil.GetSFJV(res_FddCompanyInfo.getJSONArray("data").getJSONObject(0).getJSONObject("companyVerifiedStatusVO"), "verifiedStatus");
                            verifiedStatusDesc = KHUtil.GetSFJV(res_FddCompanyInfo.getJSONArray("data").getJSONObject(0).getJSONObject("companyVerifiedStatusVO"), "verifiedStatusDesc");

                            // 将获取到的FDD企业编号和最新认证状态更新到Customer
                            i_Num = jt.update("Update Customer Set Fdd_Customerid = ?, Fdd_Status = ?, Fdd_Reason = ?, Modifiedby = 'SELITE', Modifieddate = Now(), Partner_Guid = ? " + "Where Customer_Guid = " + "?", Fdd_CompanyId, verifiedStatus, verifiedStatusDesc, Fdd_CustomerId, Cc_CustomerGuid);
                            log.debug("Fix-将FDD企业对应的信息更新到Customer-更新DB数据:" + i_Num);

                            // 识别FDD企业认证状态
                            if (verifiedStatus.equals("0")) { // 未认证
                                JSONObject res_CompanyVerify = FCallAPI_Utils.doFDDCompanyVerify(jt, Rec_Guid, Cc_CustomerId, Fdd_CompanyId, Cc_Mobile);

                                log.debug("Fix-根据企业信息 识别FDD企业认证状态-未认证,已推送认证:" + res_CompanyVerify);

                                jt.update("Insert Into Sys_Oper_Log_Auth (Oper_Id, Rec_Guid, Customer_Id, Customer_Guid, Fdd_Customer_Id, Push_Type, Push_Time) Values (Uuid(), ?, ?, ?, ?, '认证', Now())", Rec_Guid, Cc_CustomerId, Cc_CustomerGuid, Fdd_CompanyId);

                                res_Json = iAPIResult.WARN("检测到企业客户认证状态:未认证,已推送认证,认证链接:" + KHUtil.GetSFJV(res_CompanyVerify.getJSONObject("data").getJSONObject("data"), "url"), res_CompanyVerify);

                            } else if (verifiedStatus.equals("4")) { // 已认证-通过
                                log.debug("Fix-根据企业信息 识别FDD企业认证状态-已认证通过");

                                JSONObject res_JsonData = new JSONObject();
                                res_JsonData.put("Fdd_CompanyId", Fdd_CompanyId);
                                res_JsonData.put("Fdd_CustomerId", Fdd_CustomerId);
                                res_JsonData.put("verifiedStatus", verifiedStatus);
                                res_JsonData.put("verifiedStatusDesc", verifiedStatusDesc);
                                res_Json = iAPIResult.SUCCESS("检测到企业客户认证状态:已认证通过,可关闭当前页面重新打开进行下一步操作", res_JsonData);

                            } else {
                                res_Json = iAPIResult.ERROR("检测到企业客户认证状态(未定义状态):-" + verifiedStatus + "/" + verifiedStatusDesc, res_FddCompanyInfo);

                            }

                        } else { // 未查询到企业信息

                            // 先注册个人
                            JSONObject res_PersonRegi = FCallAPI_Utils.doFDDPersonRegi(jt, Rec_Guid, Cc_Mobile);

                            if (KHUtil.GetSFJV(res_PersonRegi, "code").equals("1")) {

                                JSONObject res_FddPersonRegi = res_PersonRegi.getJSONObject("data");
                                // 如果法大大返回 1成功或1001003已存在均是成功
                                if (KHUtil.GetSFJV(res_FddPersonRegi, "code").equals("1") || KHUtil.GetSFJV(res_FddPersonRegi, "code").equals("1001003")) {
                                    Fdd_CustomerId = KHUtil.GetSFJV(res_FddPersonRegi, "data");

                                    // 未查询到企业信息 新注册企业
                                    JSONObject res_CompanyRegi = FCallAPI_Utils.doFDDCompanyRegi(jt, Rec_Guid, Fdd_CustomerId, Cc_Mobile, Dbc_CustomerName);

                                    if (KHUtil.GetSFJV(res_CompanyRegi, "code").equals("1")) {

                                        JSONObject res_FddCompanyRegi = res_CompanyRegi.getJSONObject("data");
                                        // 如果法大大返回 1成功或1001003已存在均是成功
                                        if (KHUtil.GetSFJV(res_FddCompanyRegi, "code").equals("1") || KHUtil.GetSFJV(res_FddCompanyRegi, "code").equals("1001003")) {
                                            Fdd_CompanyId = KHUtil.GetSFJV(res_FddCompanyRegi, "data");

                                            // 将获取到的FDD企业编号更新到Customer
                                            i_Num = jt.update("Update Customer Set Fdd_Customerid = ?, Modifiedby = 'SELITE', Modifieddate = Now(), Partner_Guid = ? Where Customer_Guid " + "= ?", Fdd_CompanyId, Fdd_CustomerId, Cc_CustomerGuid);
                                            log.debug("Fix-新注册企业后,FDD企业编号更新到Customer-更新DB数据:" + i_Num);

                                            // 新建企业后直接推送发起认证
                                            JSONObject res_CompanyVerify = FCallAPI_Utils.doFDDCompanyVerify(jt, Rec_Guid, Cc_CustomerId, Fdd_CompanyId, Cc_Mobile);
                                            log.debug("Fix-新注册企业后直接推送发起认证,推送认证:" + res_CompanyVerify);

                                            jt.update("Insert Into Sys_Oper_Log_Auth (Oper_Id, Rec_Guid, Customer_Id, Customer_Guid, Fdd_Customer_Id, Push_Type, Push_Time) Values (Uuid(), ?, ?, ?, ?, '认证', Now())", Rec_Guid, Cc_CustomerId, Cc_CustomerGuid, Fdd_CompanyId);

                                            res_Json = iAPIResult.WARN("新注册企业客户后直接推送发起认证,已推送认证-等待认证回调,认证链接:" + KHUtil.GetSFJV(res_CompanyVerify.getJSONObject("data").getJSONObject("data"), "url"), res_CompanyVerify);

                                        } else {
                                            res_Json = iAPIResult.ERROR("调用法大大注册企业客户(法大大错误)-" + KHUtil.GetSFJV(res_FddPersonRegi, "msg"), res_FddPersonRegi);

                                        }

                                    } else {
                                        res_Json = iAPIResult.ERROR("调用法大大注册企业客户(内部错误)-" + KHUtil.GetSFJV(res_CompanyRegi, "msg"), res_CompanyRegi);
                                    }

                                } else {
                                    res_Json = iAPIResult.ERROR("调用法大大创建企业用户(法大大错误)-" + KHUtil.GetSFJV(res_FddPersonRegi, "msg"), res_FddPersonRegi);

                                }

                            } else {
                                res_Json = iAPIResult.ERROR("调用法大大创建企业用户(内部错误)-" + KHUtil.GetSFJV(res_PersonRegi, "msg"), res_PersonRegi);
                            }

                        }

                    } else {
                        res_Json = iAPIResult.ERROR("调用法大大查询企业(法大大错误)-" + KHUtil.GetSFJV(res_CompanyInfo.getJSONObject("data"), "msg"), res_CompanyInfo);

                    }
                } else {
                    res_Json = iAPIResult.ERROR("调用法大大查询企业(内部错误)-" + KHUtil.GetSFJV(res_CompanyInfo, "msg"), res_CompanyInfo);

                }

            }

        } catch (Exception e) {
            res_Json = iAPIResult.ERROR("HTTP请求调用错误，接口处理异常：" + e.getMessage());
            log.error(res_Json, e);

        }/* finally {
            LogToDB.toDB(eliteBean, "dataSource", "合同批量-控制Main", "apiBatchSignMain", callPram, res_Json.toString(), (System.currentTimeMillis() - startTime).toString(), getMethod + ":" + mapParam.toString());
        }*/

        log.debug("单用户法大大注册&认证处理Main 执行结果：" + res_Json.toString());

    }

}
